package functionalEnrichment.semanticSimilarity.semanticSimilarityCalculator.units;

/**
 * An object that stores the semntic similrity (as an integer value) between two genes.
 * @author Jamie MacPherson
 */
public class SemanticSimilarity implements Comparable {

    // the two genes in question
    private Gene  gene1;
    private Gene gene2;

    // an integer value that reports the semantic similarity
    private int similarity;

    /**
     *
     * @param g1 the first Gene object
     * @param g2 the second Gene object
     */
    public SemanticSimilarity(Gene g1, Gene g2) {

        this.gene1 = g1;
        this.gene2 = g2;
    }

    /**
     *
     * @param similarity the semntic similarity between the gene objects that are stored
     */
    public void setSimilarity(int similarity) {
        this.similarity = similarity;
    }

    /**
     * Returns true if the <code>SemanticSimilarity</code> object
     * that is being compared to this one if it has equal genes in positions gene1 and gene2.
     * If not false is returned.
     */
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final SemanticSimilarity other = (SemanticSimilarity) obj;
        if (this.gene1 != other.gene1 && (this.gene1 == null || !this.gene1.equals(other.gene1))) {
            return false;
        }
        if (this.gene2 != other.gene2 && (this.gene2 == null || !this.gene2.equals(other.gene2))) {
            return false;
        }
        return true;
    }



    
    /**
     *
     * @return a hashcode based on the two genes stored in the object.
     */
    @Override
    public int hashCode() {
        int hash = 7;
        hash = 67 * hash + (this.gene1 != null ? this.gene1.hashCode() : 0);
        hash = 67 * hash + (this.gene2 != null ? this.gene2.hashCode() : 0);
        return hash;
    }

    /**
     *
     * @return the value for the semantic similarity.
     */
    public int getSimilarity() {
        return similarity;
    }

    /**
     *
     * @return the first gene in the semantic similarity
     */
    public Gene getGene1() {
        return gene1;
    }

    /**
     *
     * @return the second gene in the semantic similarity
     */
    public Gene getGene2() {
        return gene2;
    }

    
    /**
     * Allows the gene to be sorted, firstly on the natural ordering of the first gene id,
     * then the natural ordering of the second gene id.
     * @param o the object to compare to this one (should be a gene).
     */
    public int compareTo(Object o) {
        SemanticSimilarity other = (SemanticSimilarity)o;
        if(this.getGene1().compareTo(other.getGene1())!=0){
            return this.getGene1().compareTo(other.getGene1());
        } else {
            return this.getGene2().compareTo(other.getGene2());
        }
    }


}
